package com.ws.controller;

import com.ws.common.PaasPage;
import com.ws.common.PaasResponse;
import com.ws.entity.BorrowingRecord;
import com.ws.response.BorrowingRecordPageResponse;
import com.ws.response.BorrowingRecordResponse;
import com.ws.service.BorrowingRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author ：ws
 * @date ：Created in 2023/10/22 22:00
 * @description：借阅记录
 * @modified By：
 * @version: 1.0.0
 */
@Controller
@RefreshScope
@CrossOrigin
@RequestMapping("/brrow")
public class BorrowingRecordController {
    @Autowired
    BorrowingRecordService borrowingRecordService;

    /**
     * 借还书记录查询接口
     * @param pageable
     * @param readerId
     * @return
     */
    @GetMapping("")
    public ResponseEntity<BorrowingRecordPageResponse> getBrrowingRecordPage(
            @PageableDefault(page = 1, direction = Sort.Direction.DESC) Pageable pageable,
            @RequestParam(value="readerId",required=false) Long readerId,
            @RequestParam(value="statusCd",required=false) String statusCd
    ){
        PaasPage<BorrowingRecord> brrowingRecordPaasPage = borrowingRecordService.getBorrowingRecordPage(pageable,readerId,statusCd);
        return ResponseEntity.ok(new BorrowingRecordPageResponse(brrowingRecordPaasPage));
    }

    /**
     * 借还书记录新增接口
     * @param borrowingRecord
     * @return
     */
    @PostMapping("")
    public ResponseEntity<BorrowingRecordResponse> saveBrrowingRecord(@RequestBody @Valid BorrowingRecord borrowingRecord){
        BorrowingRecord brrowingRecord1 = borrowingRecordService.saveBorrowingRecord(borrowingRecord);
        return ResponseEntity.ok(new BorrowingRecordResponse(brrowingRecord1));
    }

    /**
     * 查询对应借还书记录的基本信息
     * @param borrowingRecordId
     * @return
     */
    @GetMapping("/{borrowingRecordId}/basicInfo")
    public ResponseEntity<BorrowingRecordResponse> getBrrowingRecordBasicInfo(@PathVariable Long borrowingRecordId){
        BorrowingRecord brrowingRecord = borrowingRecordService.getBorrowingRecordBasicInfo(borrowingRecordId);
        return ResponseEntity.ok(new BorrowingRecordResponse(brrowingRecord));
    }

    /**
     * 删除对应的借还书记录
     * @param borrowingRecordId
     * @return
     */
    @PostMapping("/d/{borrowingRecordId}")
    public ResponseEntity delBorrowingRecord(@PathVariable Long borrowingRecordId)  {
        borrowingRecordService.delBorrowingRecord(borrowingRecordId);
        return ResponseEntity.ok(new PaasResponse());
    }

    /**
     * 修改对应的借还书记录
     * @param borrowingRecordId
     * @return
     */
    @PostMapping("/u/{borrowingRecordId}")
    public ResponseEntity<BorrowingRecordResponse> updateBrrowingRecord(@PathVariable Long borrowingRecordId,@RequestBody BorrowingRecord borrowingRecord)  {
        BorrowingRecord brrowingRecord1 = borrowingRecordService.updateBorrowingRecord(borrowingRecordId,borrowingRecord);
        return ResponseEntity.ok(new BorrowingRecordResponse(brrowingRecord1));
    }
}
